﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Basic4Net.Entities;

namespace Basic4Net.BusinessRules
{
    public class ListAdjust
    {
        private int _teacherid;
        private string _typename;
        private string _teachcode;
        private string _teachname;
        private float _subsidycls;
        private float _holwithhold;
        private float _withholdmoney;
        private float _holidaywork;
        private float _amountdiffer;
        private int _meritpayid;
        private string _holidayworkcontent;
        private string _amountdiffercontent;

        public int TeacherID
        {
            get { return _teacherid; }
            set { _teacherid = value; }
        }

        public string TypeName
        {
            get { return _typename; }
            set { _typename = value; }
        }

        public string TeachCode
        {
            get { return _teachcode; }
            set { _teachcode = value; }
        }

        public string TeachName
        {
            get { return _teachname; }
            set { _teachname = value; }
        }

        public float SubsidyCls
        {
            get { return _subsidycls; }
            set { _subsidycls = value; }
        }

        public float Holwithhold
        {
            get { return _holwithhold; }
            set { _holwithhold = value; }
        }

        public float WithholdMoney
        {
            get { return _withholdmoney; }
            set { _withholdmoney = value; }
        }

        public float HolidayWork
        {
            get { return _holidaywork; }
            set { _holidaywork = value; }
        }

        public float AmountDiffer
        {
            get { return _amountdiffer; }
            set { _amountdiffer = value; }
        }

        public int MeritPayID
        {
            get { return _meritpayid; }
            set { _meritpayid = value; }
        }

        public string HolidayWorkContent
        {
            get { return _holidayworkcontent; }
            set { _holidayworkcontent = value; }
        }

        public string AmountDifferContent
        {
            get { return _amountdiffercontent; }
            set { _amountdiffercontent = value; }
        }
    }

    public class Adjust
    {
        public IList<ListAdjust> ListTeacher(int FYear,int FMonth,int FType, string TeachCode, string TeachName)
        {
            using (DBEntities db = new DBEntities())
            {
                var obj = (from b in db.TEACHERINFO
                           join c in db.t_PostRelation on b.RID equals c.TeacherID into temp1
                           from d in temp1.DefaultIfEmpty()
                           join a in db.t_Post on d.PostID equals a.RID into temp2
                           from e in temp2.DefaultIfEmpty()
                           join f in db.t_MeritPay on b.RID equals f.TeacherID into temp3
                           from g in temp3.DefaultIfEmpty()
                           where
                               g.FYear == FYear && g.FMonth == FMonth &&
                            (FType == -1 ? true : (FType == 0 ? e.RID == null : e.RID == FType))
                           && (String.IsNullOrEmpty(TeachCode.Trim()) ? true : b.TEACHCODE.StartsWith(TeachCode.Trim()))
                           && (String.IsNullOrEmpty(TeachName.Trim()) ? true : b.TEACHNAME.StartsWith(TeachName.Trim()))
                           && g.TeachType != null
                           select new ListAdjust
                           {
                               TeacherID = b.RID,
                               TypeName = e.PostName != null ? e.PostName : "",
                               TeachCode = b.TEACHCODE,
                               TeachName = b.TEACHNAME,
                               SubsidyCls = g.SubsidyCls != null ? (float)g.SubsidyCls : 0,
                               Holwithhold = g.Holwithhold != null ? (float)g.Holwithhold : 0,
                               WithholdMoney = g.WithholdMoney != null ? (float)g.WithholdMoney : 0,
                               HolidayWork = g.HolidayWork != null ? (float)g.HolidayWork : 0,
                               AmountDiffer = g.AmountDiffer != null ? (float)g.AmountDiffer : 0,
                               MeritPayID = g.RID != null ? (int)g.RID : -1,
                               HolidayWorkContent = g.HolidayWorkContent,
                               AmountDifferContent = g.AmountDifferContent
                           });
                return obj.ToList();
            }
        }

        public bool ChangeSubsidyCls(int MeritPayID, float value, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                if (result)
                {
                    t_MeritPay model = db.t_MeritPay.FirstOrDefault(t => t.RID == MeritPayID);
                    model.SubsidyCls = value;
                    model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                    db.ApplyCurrentValues("t_MeritPay", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool ChangeHolidayWork(int MeritPayID, float value, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                if (result)
                {
                    t_MeritPay model = db.t_MeritPay.FirstOrDefault(t => t.RID == MeritPayID);
                    model.HolidayWork = value;
                    model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                    db.ApplyCurrentValues("t_MeritPay", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool ChangeAmountDiffer(int MeritPayID, float value, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                if (result)
                {
                    t_MeritPay model = db.t_MeritPay.FirstOrDefault(t => t.RID == MeritPayID);
                    model.AmountDiffer = value;
                    model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                    db.ApplyCurrentValues("t_MeritPay", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool ChangeHolidayWorkContent(int MeritPayID, string value, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                if (result)
                {
                    t_MeritPay model = db.t_MeritPay.FirstOrDefault(t => t.RID == MeritPayID);
                    model.HolidayWorkContent = value;
                    db.ApplyCurrentValues("t_MeritPay", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool ChangeAmountDifferContent(int MeritPayID, string value, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                if (result)
                {
                    t_MeritPay model = db.t_MeritPay.FirstOrDefault(t => t.RID == MeritPayID);
                    model.AmountDifferContent = value;
                    db.ApplyCurrentValues("t_MeritPay", model);
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool ChangeHolwithhold(int FYear,int FMonth,float value,ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                if (result)
                {
                    var obj = (from t in db.t_MeritPay.DefaultIfEmpty()
                               where t.FYear == FYear
                               && t.FMonth == FMonth
                               && t.TeachType != null
                               select t);
                    foreach (t_MeritPay model in obj.ToList())
                    {
                        model.Holwithhold = value;
                        model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                        db.ApplyCurrentValues("t_MeritPay", model);
                    }

                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool GetHolwithholdState(int FYear, int FMonth)
        {
            using (DBEntities db = new DBEntities())
            {
                t_MeritPay model = (from t in db.t_MeritPay.DefaultIfEmpty()
                                    where t.FYear == FYear
                                    && t.FMonth == FMonth
                                    select t).FirstOrDefault();
                if (model != null)
                {
                    if (model.Holwithhold == null || model.Holwithhold == 0)
                        return false;
                    else
                        return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool GetWithholdMoneyState(int FYear, int FMonth)
        {
            using (DBEntities db = new DBEntities())
            {
                t_MeritPay model = (from t in db.t_MeritPay.DefaultIfEmpty()
                                    where t.FYear == FYear
                                    && t.FMonth == FMonth
                                    select t).FirstOrDefault();
                if (model != null)
                {
                    if (model.WithholdMoney == null || model.WithholdMoney == 0)
                        return false;
                    else
                        return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool ChangeWithholdMoney(int FYear, int FMonth, bool check, ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var Appobj = (from t in db.t_Appraise.DefaultIfEmpty()
                              select t).DefaultIfEmpty();
                foreach (t_Appraise appmodel in Appobj.ToList())
                {
                    var obj = (from t1 in db.t_MeritPay
                               join t2 in db.t_AppraiseRelation on t1.TeacherID equals t2.TeacherID into temp
                               from t in temp.DefaultIfEmpty()
                               where t.AppraiseID == appmodel.RID
                               && t1.FYear == FYear
                               && t1.FMonth == FMonth
                               && t1.TeachType != null
                               select t1).DefaultIfEmpty();
                    if (check)
                    {
                        var SumCount = (from t1 in db.t_MeritPay
                                        join t2 in db.t_AppraiseRelation on t1.TeacherID equals t2.TeacherID into temp
                                        from t in temp.DefaultIfEmpty()
                                        where t.AppraiseID == appmodel.RID
                                        && t1.FYear == FYear
                                        && t1.FMonth == FMonth
                                        && t1.TeachType != null
                                        select t1).Count();
                        int i = 1;
                        foreach (t_MeritPay model in obj.ToList())
                        {
                            if (model != null)
                            {
                                if (i <= Convert.ToInt32(SumCount * 0.3))
                                {
                                    model.WithholdMoney = 800;
                                    model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                                    db.ApplyCurrentValues("t_MeritPay", model);
                                }
                                else
                                {
                                    if (i <= Convert.ToInt32(SumCount * 0.7))
                                    {
                                        model.WithholdMoney = 600;
                                        model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                                        db.ApplyCurrentValues("t_MeritPay", model);
                                    }
                                    else
                                    {
                                        model.WithholdMoney = 400;
                                        model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                                        db.ApplyCurrentValues("t_MeritPay", model);
                                    }
                                }
                            }
                            i++;
                        }
                    }
                    else
                    {
                        foreach (t_MeritPay model in obj.ToList())
                        {
                            model.WithholdMoney = 0;
                            model.AmountActual = model.AmountIssued + model.SubsidyCls - model.Holwithhold + model.WithholdMoney + model.HolidayWork + model.AmountDiffer;
                            db.ApplyCurrentValues("t_MeritPay", model);
                        }
                    }
                    try
                    {
                        db.SaveChanges();
                    }
                    catch
                    {
                        msg = "操作失败";
                        result = false;
                    }
                }
                return result;
            }
        }

        public bool ChangeAmount(int FYear,int FMonth,ref string msg)
        {
            bool result = true;
            using (DBEntities db = new DBEntities())
            {
                var obj1 = (from t in db.t_MeritPay
                            where t.FYear == FYear
                            && t.FMonth == FMonth
                            && (t.TeachType == 1 || t.TeachType == 2)
                            select t.AmountActual).Sum();
                var obj2 = (from t in db.t_MeritPay
                            where t.FYear == FYear
                            && t.FMonth == FMonth
                            && t.TeachType == 3
                            select t.AmountActual).Sum();
                t_Amount model1 = (from t in db.t_Amount
                                  where t.FYear == FYear
                                  && t.FMonth == FMonth
                                  && t.AppType == 1
                                  select t).FirstOrDefault();
                model1.PayAmount = obj1;
                model1.BalAmount = model1.AppAmount - model1.PayAmount;
                db.ApplyCurrentValues("t_Amount", model1);
                t_Amount model2 = (from t in db.t_Amount
                                   where t.FYear == FYear
                                   && t.FMonth == FMonth
                                   && t.AppType == 2
                                   select t).FirstOrDefault();
                model2.PayAmount = obj2;
                model2.BalAmount = model2.AppAmount - model2.PayAmount;
                db.ApplyCurrentValues("t_Amount", model2);
                try
                {
                    db.SaveChanges();
                }
                catch
                {
                    msg = "操作失败";
                    result = false;
                }
            }
            return result;
        }
    }
}
